//
//  FunctionMenuListViewController.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 16/12/1.
//  Copyright © 2016年 com.xtion.crm. All rights reserved.
//

#import "FunctionMenuListViewController.h"
#import "SimpleCollectionViewCell.h"
#import "CommonAddressBookContactViewController.h"
#import "PrivateAddressBookContactViewController.h"
#import "SimpleMenuListViewController.h"
#import "InnerNewsListViewController.h"
#import "AddAttendanceViewController.h"

NSString const *FunctionMenuListViewControllerMenuCodeParamKey = @"FunctionMenuListViewControllerMenuCodeParamKey";
@interface FunctionMenuListViewController ()<XUIGroupTableViewDelegate,XUIGroupTableViewDataSource,SimpleCollectionViewCellDelegate>
{
    XMSQueueDictionary *_MenuNameIndex;
    XMSQueueDictionary *_MenuIndex;
    XMSQueueDictionary *_qdData;
    RSWDataOperation *_manager;
    
    UIFont *_titleFont;

    XUIGroupTableView *_gtable;
    
    NSInteger _itemCount;
}
@end

@implementation FunctionMenuListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self checkSession];
    
    [self setNavigateItemTitle:@"首页"];
    self.view.backgroundColor = [UIColor colorWithHex:mcrmSAASUISkinColorTextStylef1f4f7Color];
    
    [self initVariable];
    
    NSString *_code = self.param[FunctionMenuListViewControllerMenuCodeParamKey];
    
    [self addKeyboardObserver];
    
    [self initUI];
    
    [_manager getSubMenuDictionaryByParentCode:_code Completion:^(NSInteger nResult, XMSQueueDictionary *output, NSError *error) {
        [self requestComplete:nil result:nResult output:output error:error]; 
    }];
}

- (void)initVariable
{
    _MenuNameIndex = [XMSQueueDictionary new];
    _MenuIndex = [XMSQueueDictionary new];
    _qdData = [XMSQueueDictionary new];
    _titleFont =[UIFont new];
    
    _manager = [[RSWDataOperation alloc]initWithDataOperationManagerDelegate:self];
    
    _itemCount = 4;
}

- (void)initUI
{
    CGFloat top = [XUINavigationController navigationController].navigationBar.bottom;
    
    _gtable = [[XUIGroupTableView alloc]initWithFrame:CGRectMake(0, top, self.view.width, self.view.height - top - ([UIDevice is58InchScreen]?83:49))];
    _gtable.backgroundColor = [[UITableView appearance] backgroundColor];
    _gtable.delegate = self;
    _gtable.dataSource = self;
    
    [self.view addSubview:_gtable];
}

-(void)viewControllerWillDealloc
{
    [super viewControllerWillDealloc];
    
    [_manager terminateRequest];
    
    [self removeObserver];
}

-(void)requestComplete:(XMSDataOperationManager *)dom result:(NSInteger)nResult output:(id)output error:(NSError *)error
{
    
    [self filterRequestResult:nResult defaultError:error noFilter:^(NSInteger requestResult)
     {
         switch(requestResult)
         {
             case YES:
             {
                 _qdData = output;
                 
                 NSInteger index = 0;
                 for (NSString *infoName in _qdData.allKeys)
                 {
                     [_MenuNameIndex addKey:@(index) value:infoName];
                     
                     [_MenuIndex addKey:@(index) value:[_qdData getValueForKey:infoName]];
                     
                     index ++;
                 }
                 
                 [_gtable reloadData];
                 
                 break;
             }
             default:
             {
                 break;
             }
         }
     }];
}


#pragma mark --
#pragma mark -- XUIGroupTableViewDelegate XUIGroupTableViewDataSource
-(CGFloat)groupTableView:(XUIGroupTableView *)groupTableView heightForHeaderInGroup:(NSInteger)group
{
    return 0;
}

- (NSUInteger)numberOfGroupsInGroupTableView:(XUIGroupTableView *)groupTableView
{
    return 1;
}

- (NSUInteger)groupTableView:(XUIGroupTableView *)groupTableView numberOfRowsInGroup:(NSInteger)group
{
    return _qdData.count;
}

- (CGFloat)groupTableView:(XUIGroupTableView *)groupTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row ==0)
    {
        NSArray *arr = [_MenuIndex getValueForIndex:indexPath.row];
        
        return [SimpleCollectionViewCell caleHeight:arr.count itemCount:_itemCount itemHeight:1 hiddenTitle:NO];
    }
    else if(indexPath.row ==1)
    {
        NSArray *arr = [_MenuIndex getValueForIndex:indexPath.row];
        return [SimpleCollectionViewCell caleHeight:arr.count itemCount:_itemCount itemHeight:1 hiddenTitle:NO];
    }
    return 44;
}

- (XUIGroupTableViewCell *)groupTableView:(XUIGroupTableView *)groupTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SimpleCollectionViewCell *cell = [[SimpleCollectionViewCell alloc] initWithFrame:CGRectZero];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
 
    cell.itemCount = _itemCount;
    cell.uiEdgeInsets = UIEdgeInsetsMake(1, 1, 1, 1);
    cell.hiddenTitle = NO;
    cell.title = [_MenuNameIndex getValueForIndex:indexPath.row];
    cell.qdData = [[_MenuIndex getValueForIndex:indexPath.row] mutableCopy];
    cell.delegate = self;
    
    return cell;
}

#pragma --
#pragma -- SimpleCollectionViewCellDelegate
-(void)simpleCollectionViewCell:(SimpleCollectionViewCell*)cell clickedButton:(id)sender info:(MenuInfo*)info;
{
    if([info.code isEqualToString:@"公共通讯录"])
    {
        XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
        p.param[CommonAddressBookContactViewControllerTitleParamKey] =  info.name;
        [XUINavigationController pushViewControllerClass:[CommonAddressBookContactViewController class] withSwitchParam:p];
    }
    else if([info.code isEqualToString:@"个人通讯录"])
    {
        XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
        p.param[PrivateAddressBookContactViewControllerTitleParamKey] =  info.name;
        [XUINavigationController pushViewControllerClass:[PrivateAddressBookContactViewController class] withSwitchParam:p];
    }
    else if([info.code isEqualToString:@"工作纪要"] ||
            [info.code isEqualToString:@"定位考勤"])
    {
        XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
        p.param[SimpleMenuListViewControllerTitleParamKey] =  info.name;
        p.param[SimpleMenuListViewControllerMenuCodeParamKey] = info.code;
        [XUINavigationController pushViewControllerClass:[SimpleMenuListViewController class] withSwitchParam:p];
    }
    else if([info.code isEqualToString:@"考勤上报"])
    {
        XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
        p.param[AddAttendanceViewControllerTitleParamKey] =  info.name;
        
        [XUINavigationController pushViewControllerClass:[AddAttendanceViewController class] withSwitchParam:p];
    }
    else if([info.code isEqualToString:@"内部新闻"])
    {
        XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
        p.param[InnerNewsListViewControllerTitleParamKey] =  info.name;
        p.param[InnerNewsListViewControllerMobileParametersParamKey] = info.mobileParameters;
        
        [XUINavigationController pushViewControllerClass:[InnerNewsListViewController class] withSwitchParam:p];
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
